package main

import (
	"communityTopics/controller"
	"communityTopics/repository"
	"communityTopics/util"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
	"strconv"
	"time"
)

func main() {
	repository.Init()
	engine := gin.Default()

	engine.GET("/community/getToken/:uid", func(c *gin.Context) {
		uidStr := c.Param("uid")
		uid, err := strconv.ParseInt(uidStr, 10, 64)
		if err != nil {
			c.JSON(http.StatusBadRequest, "")
		}
		token, _ := util.GenerateToken(uid, time.Hour*3)
		c.JSON(200, token)
	})

	engine.GET("/community/page/get/:id", func(c *gin.Context) {
		topicId := c.Param("id")
		data := controller.QueryPageInfo(topicId)
		c.JSON(200, data)
	})

	engine.POST("/community/post/publish", util.AuthMiddleware(), func(c *gin.Context) {
		userId := util.GetUserIDFromContext(c)
		topicId := c.Query("tid")
		rootId := c.Query("rid")
		content := c.PostForm("content")
		log.Printf("获取了传递过来的参数：userId=%v,ropicid=%v,rootId=%v,content=%v", userId, topicId, rootId, content)
		data := controller.PostPublish(userId, topicId, rootId, &content)
		c.JSON(200, data)
	})
	engine.Run()
}
